<?PHP
  $current_page = "register.php";
  $page_type = "";
  require_once 'header.php';
?>
	<div id="page">
	  <div id="page-padding">
        <!-- start content -->	    
	    <div id="content">
	      <div id="content-padding">
            <h1>Registration Form</h1>
            <?PHP
			  // Input validation error messages
              $input_user_id_error = "";
              $input_password_error = "";
			  $input_confirm_password_error = "";
              $input_fname_error = "";
              $input_lname_error = "";
              $input_email_error = "";
			  $input_confirm_email_error = "";
			  
			  // Reset form fields
              if (isset($_POST["clear"]) or !isset($_POST["register"])) {
                resetForm();
              }
			  
			  // Registration error message
			  if (!empty($_GET["register_error"]))
                $msg = "<font color=\"#CC0000\">{$_GET["register_error"]}</font>";
              else
                $msg = '';
				
              // Input validation
              if (isset($_POST["register"])) {

                // Validate user id
                if (empty($_POST["input_user_id"])) {
                  $_SESSION['input_user_id'] = "";
                  $input_user_id_error = '<font size="3" color="#CC0000">*Required, up to 30 characters</font>';
                }
                else {
                  $_SESSION['input_user_id'] = $_POST["input_user_id"];
                }
				
                // Validate password
                if (empty($_POST["input_password"])) {
                  $input_password_error = '<font size="3" color="#CC0000">*Required, up to 30 characters</font>';
                }
				else if (!validiatePassword($_POST["input_password"])) {
				  $_POST["input_password"] = "";
				  $input_password_error = '<font size="3" color="#CC0000">*Required 8 chars+, uppercase, lowercase, special char, and number</font>';
				}
				else {
                  $_SESSION['input_password'] = $_POST["input_password"];
                }
                
				// Validate confirm password
                if (!empty($_POST["input_confirm_password"]) and $_POST["input_confirm_password"] != $_POST["input_password"] or
				    empty($_POST["input_confirm_password"]) and !empty($_POST["input_password"])) {
					
                  $input_confirm_password_error = '<font size="3" color="#CC0000">*Passwords entered doesn\'t match</font>';
                }
				
                // Validate first name
                if (empty($_POST["input_fname"])) {
                  $_SESSION['input_fname'] = "";
                  $input_fname_error = '<font size="3" color="#CC0000">*Required</font>';
                }
                else {
                  $_SESSION['input_fname'] = $_POST["input_fname"];
                }

                // Validate last name
                if (empty($_POST["input_lname"])) {
                  $_SESSION['input_lname'] = "";
                  $input_lname_error = '<font size="3" color="#CC0000">*Required</font>';
                }
                else {
                  $_SESSION['input_lname'] = $_POST["input_lname"];
                }

                // Validate e-mail address
                if (empty($_POST["input_email"])) {
                  $_SESSION['input_email'] = "";
                  $input_email_error = '<font size="3" color="#CC0000">*Required</font>';
                }
				// Make sure e-mail address is in the right format with xxx@xxx
				else if (!validateEmail($_POST["input_email"])) {
				  $_POST["input_email"] = "";
				  $_SESSION['input_email'] = "";
                  $input_email_error = '<font size="3" color="#CC0000">*Invalid e-mail address</font>';
				}
                else {
                  $_SESSION['input_email'] = $_POST["input_email"];
                }
				
				// Validate confirm e-mail address
                if (!empty($_POST["input_confirm_email"]) and $_POST["input_confirm_email"] != $_POST["input_email"] or
				    empty($_POST["input_confirm_email"]) and !empty($_POST["input_email"])) {
                  $_SESSION['input_confirm_email'] = "";
                  $input_confirm_email_error = '<font size="3" color="#CC0000">*E-mail addresses entered doesn\'t match</font>';
                }
                else {
                  $_SESSION['input_confirm_email'] = $_POST["input_confirm_email"];
                }
              }
			  
			  // Make sure all fields are filled in
              if (!empty($_POST["input_user_id"]) and !empty($_POST["input_password"]) and !empty($_POST["input_confirm_password"]) and !empty($_POST["input_fname"]) and 
			      !empty($_POST["input_lname"]) and !empty($_POST["input_email"]) and !empty($_POST["input_confirm_email"])) {

                // Validated, redirect to register_response.php
                header("Location:register_response.php");
              }
              else {

                // Display registration form
                echo("<form method=\"post\" action=\"register.php\">
                       Username: <input type=\"text\" value =\"{$_SESSION['input_user_id']}\" size=\"20\" maxlength=\"30\" name=\"input_user_id\">$input_user_id_error<br/> 
					   Password: <input type=\"password\" value =\"\" size=\"20\" maxlength=\"30\" name=\"input_password\">$input_password_error<br/> 
					   Confirm Password: <input type=\"password\" value =\"\" size=\"20\" maxlength=\"30\" name=\"input_confirm_password\">$input_confirm_password_error<br/> 
					   First Name: <input type=\"text\" value =\"{$_SESSION['input_fname']}\" size=\"20\" maxlength=\"30\" name=\"input_fname\">$input_fname_error<br/>
					   Last Name: <input type=\"text\" value =\"{$_SESSION['input_lname']}\" size=\"20\" maxlength=\"30\" name=\"input_lname\">$input_lname_error<br/>
					   E-mail: <input type=\"text\" value =\"{$_SESSION['input_email']}\" size=\"20\" maxlength=\"50\" name=\"input_email\">$input_email_error<br/>
					   Confirm E-mail: <input type=\"text\" value =\"{$_SESSION['input_confirm_email']}\" size=\"20\" maxlength=\"50\" name=\"input_confirm_email\">$input_confirm_email_error<br/><br>
					   <input type=\"submit\" name=\"register\" value=\"Register\">
	                   <input type=\"submit\" name=\"clear\" value=\"Clear\">&nbsp&nbsp$msg
                      </form>");
              }
			?>
            <p>&nbsp; </p>
	      </div>
		</div>
	  <!-- end content --></div>
	  <div id="footer">
	    <div id="footer-pad">
	      <div class="line"></div>
		  <!-- footer and copyright notice -->
	      <p>Design by <a href="http://www.steves-templates.com">Steve's free website templates</a>.</p>
		  <!-- end footer and copyright notice -->
	    </div>
	  </div>
	</div>
  </body>
</html>